<template>
  <section>
    <el-row style="margin: -3px 0px 0px 1px">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/system/dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>配件管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" style="float:left;margin: 18px 27px -3px">
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <stock-query @SearchByName="listStock" ref="searchBar"/>
        </el-form-item>
      </el-form>
    </el-col>

    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        :data="stockList"
        stripe
        style="width: 100%"
        @selection-change="selectChange"
        :max-height="tableHeight">
        <!--Form-->
        <el-table-column prop="name" label="名称" width="100" fixed/>
        <el-table-column prop="description" label="描述" show-overflow-tooltip="true"/>
        <el-table-column prop="supplier" label="供应商" show-overflow-tooltip="true"/>
        <el-table-column prop="remainAmount" label="剩余数量(个)">
          <template slot-scope="scope">
            <span v-if="scope.row.remainAmount<=scope.row.warn" style="color:#d7133c"/>
            <span v-else style="color:green">{{ scope.row.remainAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="warehousingDate" label="入库时间" show-overflow-tooltip="true"/>
        <el-table-column prop="purchasePrice" label="进货总价（元）"/>
        <el-table-column prop="sellPrice" label="出售单价（元）"/>
        <el-table-column prop="enable" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="(value) => commitStatusChange(value, scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="editStock(scope.row)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="deleteStock(scope.row)">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-col :span="24" class="toolbar">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[5, 10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </el-col>
      </el-col>
    </el-card>
    <stock-add-or-edit ref="StockAU" @reload="listStock"/>
  </section>
</template>

<script>

import StockAddOrEdit from "./stockcomponent/StockAddOrEdit";
import StockQuery from "./stockcomponent/StockQuery";

export default {
  name: "Stock",
  components: {StockQuery, StockAddOrEdit},
  data() {
    return {
      stockList: [],
      dialogFormVisible: false,
      roles: null,
      size: 5,
      total: 0,
      page: 1,
    }
  },

  mounted() {
    this.listStock();
  },

  computed: {
    tableHeight() {
      return window.innerHeight - 320
    }
  },

  methods: {
    listStock() {
      this.$axios.post('/content/stock/listStock', {
        page: this.page,
        size: this.size,
        name: this.$refs.searchBar.name
      }).then(resp => {
        if (resp.status === 200 && resp) {
          this.stockList = resp.data.result;
          this.total = resp.data.total
        }
      })
    },
    handleAdd() {
      this.$refs.StockAU.dialogFormVisible = true
      this.$refs.StockAU.title = '添加配件'
    },
    editStock(stock) {
      this.$refs.StockAU.edit(stock)
      this.$refs.StockAU.dialogFormVisible = true
      this.$refs.StockAU.title = '修改配件'
    },
    deleteStock(row) {
      this.$confirm("确定删除该条信息吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning"
      }).then(() => {
        if (row.enable === true) {
          this.$message({
            message: row.name + "已上架不能删除",
            type: "warning"
          })
          this.listStock()
        } else {
          this.$axios.post("/content/stock/delete", row).then(resp => {
            if (resp.data.code === 200) {
              this.$message({
                message: resp.data.result,
                type: "success"
              })
              this.listStock()
            }
          })
        }
      })
    },
    commitStatusChange(value, stock) {
      this.$axios.put('/content/stock/changeEnable', stock).then(resp => {
        if (resp.data.code === 200) {
          if (value) {
            this.$message({
              message: resp.data.result,
              type: "success"
            })
          } else {
            this.$message({
              message: resp.data.result,
              type: "warning"
            })
          }
        }
      })
    },
    selectChange(checked) {
      this.checked = checked
    },
    handleCurrentChange(page) {
      this.page = page
      this.listStock()
    },
    handleSizeChange(val) {
      this.size = val
      this.listStock()
    }
  }
}
</script>

<style scoped>

</style>
